<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

// 门户模块公共函数库

use Overtrue\Pinyin\Pinyin;
use think\Db;

use function PHPSTORM_META\type;

if (!function_exists('isdir')) {
    /**
     * 判断目录是否存在
     * @param $extractToDir
     * @return bool
     * @author 蔡伟明 <314013107@qq.com>
     */
    function isdir($extractToDir)
    {

        // 检查目录是否存在
        if (!is_dir($extractToDir)) {
            // 目录不存在，创建目录
            if (!mkdir($extractToDir, 0755, true)) {
                return false;
            }
            return true;
        } else {
            return true;
        }
    }
}
if (!function_exists('moveFilesToDirectory')) {


    /**
     * 移动文件到指定目录
     * @param $sourceDir 源文件目录
     * @param $targetDir 目标文件目录
     */
    function moveFilesToDirectory($sourceDir, $targetDir)
    {
        if (!is_dir($sourceDir) || !is_dir($targetDir)) {
            @isdir($sourceDir);
            @isdir($targetDir);
        }

        $files = scandir($sourceDir);

        foreach ($files as $file) {
            if ($file == '.' || $file == '..') {
                continue;
            }

            $sourcePath = $sourceDir . DIRECTORY_SEPARATOR . $file;
            $targetPath = $targetDir . DIRECTORY_SEPARATOR . $file;
            @isdir($sourceDir);
            @isdir($targetDir);
            if (is_dir($sourcePath)) {
                // 如果是目录，递归调用moveFilesToDirectory函数
                moveFilesToDirectory($sourcePath, $targetPath);
                // 移动空目录
                rmdir($sourcePath);
            } else {
                // 如果是文件，使用rename函数移动文件

                rename($sourcePath, $targetPath);
            }
        }
    }
}


if (!function_exists('parseTabularText')) {

    /**
     * 解析表格文本数据
     *
     * @param string $text
     * @return array
     */
    function parseTabularText($text)
    {
        // 按行分割文本数据
        $rows = explode("\n", $text);

        // 初始化二维数组
        $data = [];

        // 解析每行数据并构建二维数组
        foreach ($rows as $row) {
            $columns = explode("\t", $row);
            $data[] = $columns;
        }

        return $data;
    }
}
if (!function_exists('parseTabularText')) {

    function sendPostRequest($url, $fields)
    {


        $fields = array(
            'file' => new CurlFile('默认标题__2024-02-29+19_22_37.png', 'image/png'),
            'model' => 'u2net',
            'a' => 'false',
            'af' => 240,
            'ab' => 10,
            'ae' => 10,
            'om' => 'false',
            'ppm' => 'false'
        );
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 设置请求头
        $headers = array(
            'accept: application/json',
            'Content-Type: multipart/form-data'
        );
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        // 指定POST数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

        // 执行并获取HTML文档内容
        $output = curl_exec($ch);

        // 关闭CURL会话
        curl_close($ch);

        return $output;
    }
}


function goods_color_image($value, $data)
{
    $basePath = env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'goods';
    $main_img_dir = $basePath . DIRECTORY_SEPARATOR . $data['goods_code_alias'] . DIRECTORY_SEPARATOR . '颜色图';
    $file_path = getFileByFilename($main_img_dir, '颜色图_' . $data['goods_color_id']);
    $EXTENSION = pathinfo($file_path, PATHINFO_EXTENSION);
    $main_img_dir .= DIRECTORY_SEPARATOR . '颜色图_' . $data['goods_color_id'] . '.' . $EXTENSION;
    $file_path = str_replace($basePath, DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'goods', $main_img_dir);
    $file_path = str_replace('\\', '/', $file_path);

    return '<div class="js-gallery"><img class="image" data-original="' . $file_path . '" src="' . $file_path . '"></div>';
}
